package com.ruoyi.business.controller;

import cn.hutool.core.comparator.CompareUtil;
import com.ruoyi.business.domain.TUserOptLog;
import com.ruoyi.business.enums.GraduationAuditStatusEnums;
import com.ruoyi.business.enums.NoticeStatusEnums;
import com.ruoyi.business.service.ITUserOptLogService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.TRegisterNoticeVo;
import com.ruoyi.business.service.ITRegisterNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 录取通知Controller
 *
 * @author Tellsea
 * @date 2024-01-26
 */
@Api(tags = "录取通知Controller")
@RestController
@RequestMapping("/business/registerNotice")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TRegisterNoticeController extends BaseController {

    private final ITRegisterNoticeService tRegisterNoticeService;

    private final ITUserOptLogService tUserOptLogService;

    @ApiOperation("查询录取通知列表")
    @PreAuthorize("@ss.hasPermi('business:registerNotice:list')")
    @GetMapping("/list")
    public TableDataInfo<TRegisterNoticeVo> list(TRegisterNoticeVo entity) {
        return tRegisterNoticeService.queryList(entity);
    }

    @ApiOperation("查询录取通知所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TRegisterNoticeVo entity) {
        return AjaxResult.success("查询成功", tRegisterNoticeService.queryAll(entity));
    }

    @ApiOperation("导出录取通知列表")
    @PreAuthorize("@ss.hasPermi('business:registerNotice:export')")
    @Log(title = "录取通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TRegisterNoticeVo entity) {
        List<TRegisterNoticeVo> list = tRegisterNoticeService.queryAll(entity);
        ExcelUtil<TRegisterNoticeVo> util = new ExcelUtil<>(TRegisterNoticeVo.class);
        util.exportExcel(response, list, "录取通知数据");
    }

    @ApiOperation("获取录取通知详细信息")
    @PreAuthorize("@ss.hasPermi('business:registerNotice:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tRegisterNoticeService.queryById(id));
    }

    @ApiOperation("获取录取通知详细信息")
    @PreAuthorize("@ss.hasPermi('business:registerNotice:query')")
    @GetMapping(value = "/getSendNotice/{id}")
    public AjaxResult getSendNotice(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tRegisterNoticeService.queryByRegisterId(id));
    }

    @ApiOperation("新增录取通知")
    @PreAuthorize("@ss.hasPermi('business:registerNotice:add')")
    @Log(title = "录取通知", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TRegisterNoticeVo entity) {
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        List<String> roleList = new ArrayList<>();
        if(loginUser.getRoles()!=null){
            for (SysRole role : loginUser.getRoles()) {
                roleList.add(role.getRoleName());
            }
        }
        String optDesc = "发放通知书";
        TUserOptLog tUserOptLog = new TUserOptLog()
                .setOptDesc(optDesc)
                .setOptTime(DateUtils.getNowDate())
                .setFromUserid(loginUser.getUserId().toString())
                .setFromUsername(loginUser.getNickName())
                .setFromRolename(StringUtils.join(roleList,","))
                .setToUserid(entity.getRegisterId().toString());
        tUserOptLogService.save(tUserOptLog);
        return toAjax(tRegisterNoticeService.save(entity));
    }

    @ApiOperation("修改录取通知")
    @PreAuthorize("@ss.hasPermi('business:registerNotice:edit')")
    @Log(title = "录取通知", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TRegisterNoticeVo entity) {
        return toAjax(tRegisterNoticeService.updateById(entity));
    }

    @ApiOperation("删除录取通知")
    @PreAuthorize("@ss.hasPermi('business:registerNotice:remove')")
    @Log(title = "录取通知", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tRegisterNoticeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
